﻿Public Class frmTonDau
    Private ctrlDanhMuc As New CDanhMucController
    Private ctlTonNganSach As New CTonNganSachController
    Private currTonNganSach As CTonNganSachInfo = Nothing
    Private curDonVi As Integer = -1
    Private sSearch As String = ""

    Private Sub ucCountries_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CViewer.BindDonViToComboBox(cboDonVi)
        BindAutoCompleteToCode()
        cboDonVi.SelectedIndex = 0
        'hien thi nut them, xoa, sua, thoat
        HienThiNut_Thuong()
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Private Sub BindAutoCompleteToCode()
        'tạo chức năng auto complete cho Cont No
        Dim source = New AutoCompleteStringCollection()

        Dim CodeList = ctrlDanhMuc.SelectAllCode()
        source.AddRange(CodeList)

        'Textbox MBLNo
        txtCode.AutoCompleteSource = AutoCompleteSource.CustomSource
        txtCode.AutoCompleteCustomSource = source
        txtCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    End Sub
    'Private Sub frmTonNganSach_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    '    If (e.KeyCode = Keys.N AndAlso e.Modifiers = Keys.Control) Then
    '        'thêm mới
    '        btnAdd_Click(Nothing, Nothing)
    '    End If
    '    If (e.KeyCode = Keys.E AndAlso e.Modifiers = Keys.Control) Then
    '        'Sửa
    '        btnEdit_Click(Nothing, Nothing)
    '    End If
    '    If (e.KeyCode = Keys.Delete) Then
    '        'xoá
    '        btnDel_Click(Nothing, Nothing)
    '    End If
    'End Sub
#Region "Button Event"
    Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
        If (MessageBox.Show("Bạn có chắc muốn xoá tiền của ngân sách " + currTonNganSach.Ma + " ?", "Xác nhận", MessageBoxButtons.YesNo) = vbYes) Then
            ctlTonNganSach.DeleteRecord(currTonNganSach.ID_DanhMuc, curDonVi)
            'load lại grid
            LoadListView()
        End If
    End Sub
    Private Function LuuThongTin()
        'lấy ra mã danh mục
        Dim curDanhMuc As tblDanhMuc = ctrlDanhMuc.SelectRecordByCode(txtCode.Text)
        If (curDanhMuc Is Nothing) Then
            lblTenKinhPhi.Text = "Nhập danh mục chưa đúng!"
            Return False
        End If
        'luu du lieu 
        ctlTonNganSach.DeleteRecord(curDanhMuc.ID_DanhMuc, curDonVi)
        With currTonNganSach
            .ID_DanhMuc = curDanhMuc.ID_DanhMuc
            .NoiDung = curDanhMuc.NoiDung
            If (txtBVCanDoi.Text <> "") Then
                .BVCanDoi = CDbl(txtBVCanDoi.Text)
            End If
            If (txtHienVat.Text <> "") Then
                .HienVat = CDbl(txtHienVat.Text)
            End If
            If (txtKhac.Text <> "") Then
                .Khac = CDbl(txtKhac.Text)
            End If
            If (txtTuChi.Text <> "") Then
                .TuChi = CDbl(txtTuChi.Text)
            End If
        End With
        If (currTonNganSach.BVCanDoi = 0 And currTonNganSach.HienVat = 0 And currTonNganSach.Khac And currTonNganSach.TuChi = 0) Then
            Return False
        End If
        ctlTonNganSach.InsertRecord(currTonNganSach, curDonVi)
        LoadListView()
        'Dim indexRow As Integer = -1
        ''lấy ra vị trí của port vừa thêm hoặc chỉnh sửa vào
        'indexRow = FindIndexOfPortInGrid(idCurr)
        ''Set current row
        ''gridTonNganSach.CurrentCell = gridTonNganSach.Rows(indexRow).Cells(1)
        ''gridTonNganSach.Rows(indexRow).Selected = True
        ''tro ve trạng thái thường
        HienThiNut_Thuong()
        Return True
    End Function
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        LuuThongTin()
    End Sub
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        HienThiNut_Sua()
        RefreshForm()
        'Init obj
        currTonNganSach = New CTonNganSachInfo
        txtCode.Focus()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        'gridTonNganSach.ReadOnly = False
        'gridTonNganSach.AllowUserToAddRows = False
        HienThiNut_Sua()
        txtCode.Focus()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        'Dim _id As Integer = gridTonNganSach.CurrentRow.Cells("ID_TonNganSach").Value
        'currTonNganSach = ctlTonNganSach.SelectRecordByID(_id)
        FillTonNganSachForm()
        'hien thi lai trang thai binh thuong
        HienThiNut_Thuong()
    End Sub
#End Region

#Region "Utilities"
    'Private Function FindIndexOfPortInGrid(ByVal id As Integer) As Integer
    '    Dim index As Integer = 0
    '    For Each row As DataGridViewRow In gridTonNganSach.Rows
    '        If (row.Cells("ID_TonNganSach").Value = id) Then
    '            index = row.Index
    '            Exit For
    '        End If
    '    Next
    '    Return index
    'End Function
    Private Sub FillTonNganSachForm()
        txtCode.Text = currTonNganSach.Ma
        txtBVCanDoi.Text = currTonNganSach.BVCanDoi
        txtHienVat.Text = currTonNganSach.HienVat
        txtKhac.Text = currTonNganSach.Khac
        txtTuChi.Text = currTonNganSach.TuChi
        lblTenKinhPhi.Text = currTonNganSach.NoiDung
    End Sub
    Public Sub RefreshForm()
        txtCode.Text = ""
        txtBVCanDoi.Text = ""
        txtHienVat.Text = ""
        txtKhac.Text = ""
        txtTuChi.Text = ""
        lblTenKinhPhi.Text = ""
    End Sub
    Private Sub EnableControls(ByVal val As Boolean)
        lvTonNganSach.Enabled = Not val
        txtCode.Enabled = val
        txtBVCanDoi.Enabled = val
        txtHienVat.Enabled = val
        txtKhac.Enabled = val
        txtTuChi.Enabled = val
    End Sub
    Private Sub HienThiNut_Thuong()
        'gridTonNganSach.AllowUserToAddRows = False
        'gridTonNganSach.AllowUserToDeleteRows = False
        'gridTonNganSach.ReadOnly = True
        EnableControls(False)
        btnAdd.Enabled = True
        btnDel.Enabled = True
        btnEdit.Enabled = True
        btnUpdate.Enabled = False
        btnCancel.Enabled = False
        btTiep.Enabled = False
    End Sub
    Private Sub HienThiNut_Sua()
        EnableControls(True)
        btnAdd.Enabled = False
        btnDel.Enabled = False
        btnEdit.Enabled = False
        btnUpdate.Enabled = True
        btnCancel.Enabled = True
        btTiep.Enabled = True
    End Sub
#End Region

#Region "ListView"
    Public Sub LoadListView()
        RefreshForm()
        lvTonNganSach.BeginUpdate()
        lvTonNganSach.Items.Clear()
        'lay ra tat ca cac danh muc con thuoc ma cha do
        Dim list = ctlTonNganSach.SelectAllRecord(curDonVi)
        For Each d As sp_GetPhanBoNganSachNamResult In list
            Dim item As New ListViewItem(d.Ma)
            Dim tong As Double = d.TuChi + d.HienVat + d.BVCanDoi + d.Khac
            item.SubItems.Add(d.NoiDung)
            item.SubItems.Add(d.TuChi.Value.ToString("#,##0.##"))
            item.SubItems.Add(d.HienVat.Value.ToString("#,##0.##"))
            item.SubItems.Add(d.BVCanDoi.Value.ToString("#,##0.##"))
            item.SubItems.Add(d.Khac.Value.ToString("#,##0.##"))
            item.SubItems.Add(tong.ToString("#,##0.##"))
            item.Name = d.ID_DanhMuc
            lvTonNganSach.Items.Add(item)
        Next
        lvTonNganSach.EndUpdate()
        If (lvTonNganSach.Items.Count > 0) Then
            lvTonNganSach.Items(0).Selected = True
        End If
    End Sub
    Private Sub lvChungTu_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvTonNganSach.SelectedIndexChanged
        If (lvTonNganSach.SelectedItems.Count > 0) Then
            currTonNganSach = New CTonNganSachInfo()
            With currTonNganSach
                .ID_DanhMuc = Integer.Parse(lvTonNganSach.SelectedItems(0).Name.ToString)
                .Ma = lvTonNganSach.SelectedItems(0).SubItems(0).Text
                .NoiDung = lvTonNganSach.SelectedItems(0).SubItems(1).Text
                .TuChi = CDbl(lvTonNganSach.SelectedItems(0).SubItems(2).Text)
                .HienVat = CDbl(lvTonNganSach.SelectedItems(0).SubItems(3).Text)
                .BVCanDoi = CDbl(lvTonNganSach.SelectedItems(0).SubItems(4).Text)
                .Khac = CDbl(lvTonNganSach.SelectedItems(0).SubItems(5).Text)
            End With
            'lấy thông tin danh muc
            FillTonNganSachForm()
        Else
            currTonNganSach = Nothing
        End If
    End Sub
#End Region

    Private Sub cboDonVi_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDonVi.SelectedIndexChanged
        curDonVi = cboDonVi.SelectedValue
        LoadListView()
    End Sub

    Private Sub txtTuChi_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTuChi.TextChanged, txtKhac.TextChanged, txtHienVat.TextChanged, txtBVCanDoi.TextChanged
        Dim txt As TextBox = sender
        CUtilities.FormatCurr(txt)
    End Sub
    Private Sub txtCode_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCode.Leave
        Dim curDanhMuc As tblDanhMuc = ctrlDanhMuc.SelectRecordByCode(txtCode.Text)
        If (curDanhMuc IsNot Nothing) Then
            lblTenKinhPhi.Text = curDanhMuc.NoiDung
        Else
            lblTenKinhPhi.Text = ""
            txtCode.Text = ""
            MessageBox.Show("Không đọc được thông tin danh mục!")
        End If
    End Sub

    Private Sub btTiep_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btTiep.Click
        If (LuuThongTin()) Then
            btnAdd_Click(Nothing, Nothing)
        End If
    End Sub
End Class